﻿using System;
using System.Linq;
using UnityEngine;
using UnityEngine.Networking;

public class AITargetDetector : AIBehaviour
{
    private Collider[] enteredColliders = new Collider[20];
    public LayerMask layerMask;
    private GameObject m_target;
    public bool penetrateWalls;
    public GameTagMask tagMask;
    public float triggerRadius = 100f;

    protected override void Awake()
    {
        base.Awake();
        this.layerMask = (LayerMask) GameLayers.Character;
    }

    public bool IsEmeny(GameObject go)
    {
        if (go == null)
        {
            return false;
        }
        if (!this.layerMask.ContainsFlag(go.layer))
        {
            return false;
        }
        if (!this.tagMask.IsMasked(go.tag))
        {
            return false;
        }
        if (go == base.self)
        {
            return false;
        }
        if (!this.penetrateWalls && Physics.Linecast(base.transform.position, go.transform.position, (int) GameLayers.Wall))
        {
            return false;
        }
        return true;
    }

    public override void OnDeserialize(NetworkReader reader, bool initialState)
    {
        base.OnDeserialize(reader, initialState);
    }

    public override bool OnSerialize(NetworkWriter writer, bool forceAll)
    {
        bool flag;
        bool flag2 = base.OnSerialize(writer, forceAll);
        return (flag | flag2);
    }

    protected override void Start()
    {
        base.Start();
        Life component = base.GetComponent<Life>();
        if (component != null)
        {
            component.onTakeCombat += delegate (Combat combat) {
                if ((combat.attacker != null) && this.IsEmeny(combat.attacker.gameObject))
                {
                    this.target = combat.attacker.gameObject;
                }
            };
        }
    }

    private void UNetVersion()
    {
    }

    private void Update()
    {
        if (!GameUtility.isLevelEditorScene && ((Time.frameCount % UnityEngine.Random.Range(30, 60)) == 0))
        {
            GameObject target = this.target;
            if ((this.targetLife != null) && this.targetLife.isDied)
            {
                target = null;
            }
            if ((target != null) && (target.layer == GameLayers.Effect.index))
            {
                target = null;
            }
            if (target.GetRoot() == base.self)
            {
                target = null;
            }
            if (target == null)
            {
                Physics.OverlapSphereNonAlloc(base.transform.position, this.triggerRadius, this.enteredColliders, (int) this.layerMask, QueryTriggerInteraction.Ignore);
                Collider comp = this.enteredColliders.FirstOrDefault<Collider>(x => (x != null) && this.IsEmeny(x.GetRoot()));
                if (comp != null)
                {
                    target = comp.GetRoot();
                }
            }
            else if (Vector3.Distance(target.transform.position, base.transform.position) > (this.triggerRadius + 5f))
            {
                target = null;
            }
            this.target = target;
        }
    }

    public GameObject target
    {
        get
        {
            return this.m_target;
        }
        set
        {
            this.m_target = value;
        }
    }

    private Life targetLife
    {
        get
        {
            return ((this.target == null) ? null : this.target.GetComponent<Life>());
        }
    }
}

